/**
 * Created by shaoting on 16/10/17.
 */
/**
 * Created by shaoting on 16/10/17.
 */
import React, { Component } from 'react';
import {
    AppRegistry,
    StyleSheet,
    Text,
    View,
    ListView,
    Image,
    TouchableOpacity,
    Platform,
    Dimensions,
    AsyncStorage
} from 'react-native';
var Wine = require('../Mine/Wine.json')
var {width,height} = Dimensions.get('window').height; //屏宽
var DetailsMiddleView = React.createClass({
    getInitialState(){
        return {
            product_id:'',
        }
    },
    componentDidMount(){
        var token = '';
        try {
            AsyncStorage.getItem(
                'token',
                (error,result)=>{
                    if (error){
                        alert('取值失败:'+error);
                    }else{
                        // alert('取值成功:'+result);
                        this.token = result
                    }
                }
            )
        }catch(error){
            alert('失败'+error);
        }
        let formData = new FormData();
        formData.append("product_id", this.props.product_id);
        formData.append("start", '0');
        formData.append("limit", '5');
        var fetchOptions = {
            method: 'POST',
            headers: {
                'token': this.token
            },
            body:formData,
        };

        let url = 'http://www.pintasty.cn/comment/listcomments';
        fetch(url, fetchOptions)
            .then((response) => response.json())
            .then((json) => {
                console.log(json)
                if(json.status == 200){
                    this.setState({
                        dataSource:this.state.dataSource.cloneWithRows(json.data),
                    })
                }else  if (json.status == 301){
                    // alert('没有token')
                }else {
                    alert('访问服务器失败')
                }
            }) .done();
    },
    getInitialState(){
        // 初始化数据源
        return{
            dataSource: new ListView.DataSource({rowHasChanged: (row1, row2) => row1 !== row2})
        }
    },
    render() {
        return (
            <View style={{top:-40}}>
                <View style={{marginTop:5}}>
                    <Text style={{fontSize:19}}>网友点评   (7条评论)</Text>
                    <View style={{width:width ,height: 1,backgroundColor:'rgba(200,200,200,1.0)'}}></View>
                </View>
                <ListView
                    dataSource={this.state.dataSource} //数据源
                    renderRow={this.renderRow}
                />
            </View>
        );
    },
    renderRow(rowData,sectionID,rowID,highlightRow){
        return(
            <View>
                <View style={{flexDirection:'row',marginTop:5,marginBottom: 5}}>
                    <View>
                        <Image source={{uri:rowData.comment_user_photo,}} style={{width:30,height:30,marginTop:10,marginLeft:10,borderRadius:15}}/>
                    </View>
                    <View>
                        <View style={{marginLeft:5}}>
                            <Text style={{color:'red',marginBottom: 5}}>{rowData.comment_user_name}</Text>
                            <Text style={{color:'rgba(200,200,200,1.0)',fontSize:10,marginBottom: 5}}>评论时间:   +{rowData.comment_create_time}</Text>
                            <Text style={{marginBottom: 5}}>{rowData.comment_content}</Text>
                        </View>
                        <View style={{flexDirection:'row',marginLeft:5}} >
                            <Image source={{uri:'tel_icon'}} style={{width:15,height:15}} />
                            <Text style={{marginLeft:3}}>{rowData.comment_applaud_count}</Text>
                            <Image source={{uri:'set_icon'}} style={{width:15,height:15,marginLeft:50}} />
                            <Text style={{marginLeft:3}}>{rowData.comment_reply_count}</Text>
                        </View>
                    </View>
                    <View style={{flexDirection:'row',alignItems:'center',position:'absolute',right:10}}>
                        <Text style={{fontSize:10}}>他的评分: </Text>
                        <Image source={{uri:'五星'}} style={{width:70,height:10}}/>
                    </View>
                </View>
                <View style={{width:width ,height: 1,backgroundColor:'rgba(200,200,200,1.0)'}} ></View>
            </View>
        )
    }
});


var styles = StyleSheet.create({
    container:{
        top:-39,
    }
});

module.exports = DetailsMiddleView;